package com.itheima.io.mycharstream;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class 随机点名器 {
    public static void main(String[] args) throws IOException {
        //使用缓冲字符流不断的获取name.txt文件,一读读一整行.将所有的人名存入一个集合中
        BufferedReader bufferedReader = new BufferedReader(new FileReader("day12_IO流_字符流\\name.txt"));

        ArrayList<String> names = new ArrayList<>();

        String name;
        while ((name = bufferedReader.readLine()) != null){
            names.add(name);
        }
        bufferedReader.close();

        //通过Random生成集合的随机索引
        Random r = new Random();
        int index = r.nextInt(names.size());

        //基于随机索引值来访问对应的元素,实现随机点名
        System.out.println("本次抽到的幸运观众是 : " + names.get(index));
    }
}
